<?php

class ImgLib extends MY_Controller{

    public function __construct(){
        parent::__construct();
    }

    /**
     * 添加图片
     */
    public function add_img(){
        $pageTitle = "添加图片";
        $related_id = $this->input->get_post("id");
        $type = $this->input->get_post("type")?$this->input->get_post("type"):1;
        if($_REQUEST["submits"]){
            $path = $this->input->get_post("path");
            $related_id = $this->input->get_post("id");
            if(empty($path)){
                showMessage("index.php?c=imgLib&m=img_lib", -1, "请上传图片");
            }
            foreach($path as $url_path){
                $imgInfo = getimagesize('http://nuanguang-img.oss-cn-shanghai.aliyuncs.com/'.$url_path);
                $w = $imgInfo[0] ? $imgInfo[0] : "";
                $h = $imgInfo[1] ? $imgInfo[1] : "";
                $imgsize=get_headers('http://nuanguang-img.oss-cn-shanghai.aliyuncs.com/'.$url_path,true);
                $param = array(
                    "related_id"=>$related_id,
                    "path" => 'http://nuanguang-img.oss-cn-shanghai.aliyuncs.com/'.$url_path,
                    "w" => $w, "h" => $h,"type"=>$type, "add_time" => time(),'size'=>$imgsize['Content-Length']
                );
                $insert_id[]=$this->ImgModel->insert_one($param);
            }
            //更新帖子表
            if($insert_id & $type == 1){
                $pics=implode(',',$insert_id);
                $this->PostModel->update_data(array('pics'=>$pics,'pic_id'=>$insert_id[0]),array('id'=>$related_id));
            }
            $this->AdminOpenationModel->insertLog($related_id,8,'为帖子上传添加一个封面图片,图片id：'.$pics);
            echo json_encode(array('status'=>1,'id'=>$insert_id[0]));exit;
        }
        $this->load->view('img_lib/add_img', array(
            'pageTitle' => $pageTitle,
            'related_id' => $related_id,
            'type' => $type,
        ));
    }

    /**
     * 删除图片
     */
    public function del_up_img(){
        $id = $this->input->get_post("id");
        $this->ImgModel->del_data(array("id" => $id));
        std_out(0,"");
    }

    public function del_img(){
        $post_id = $this->input->get_post("post_id");
        if(empty($post_id)){
            echo json_encode(array('status'=>-1));exit;
        }
        $this->ImgModel->update_data(array('related_id'=>0),array("related_id" => $post_id));
        $this->PostModel->update_data(array('pic_id'=>0,'pics'=>0),array("id" => $post_id));
        $this->AdminOpenationModel->insertLog($post_id,6,'删除帖子图片');
        echo json_encode(array('status'=>1));exit;
    }

    /**
     * 图片库
     */
    function img_list(){
        $pageTitle = "图片选择";
        $id = $this->input->get_post("id");
        $directory = $this->input->get_post("directory");
        $is_me     = $this->input->get_post("is_me");
        $category     = $this->input->get_post("category");
        $page      = $this->input->get_post("page") ? $this->input->get_post("page") : 1;
        $pagesize  = $this->input->get_post("pagesize") ? $this->input->get_post("pagesize") : 30;

        $where = array();
        if(!empty($directory)){
            $where["directory"] = $directory;
        }
        if($is_me){
            $loginUser = getLoginUser();
            $where["uid"] = $loginUser["uid"];
        }
        if($category == 1){
            $where["category"] = 0;
        }else if($category == 2){
            $where["category"] = 1;
        }
        $imgList = $this->ImgLibModel->getList($where, $page, $pagesize);
        $count = $this->ImgLibModel->getListCnt($where);

        $url = 'index.php?c=imgLib&m=ajax_img_list&id='.$id.'&directory='.$directory."&is_me=".$is_me."&category=".$category;
        $multipage = ajax_multipage($count, $page, $pagesize, $url, "data_img_list", "page_img_list");

        $this->load->view('img_lib/img_list', array(
            'pageTitle' => $pageTitle,
            "imgList" => $imgList,
            "multipage" => $multipage,
            "id" => $id,
        ));
    }
    public function ajax_img_list(){
        $id = $this->input->get_post("id");
        $directory = $this->input->get_post("directory");
        $is_me     = $this->input->get_post("is_me");
        $category     = $this->input->get_post("category");
        $page      = $this->input->get_post("page") ? $this->input->get_post("page") : 1;
        $pagesize  = $this->input->get_post("pagesize") ? $this->input->get_post("pagesize") : 30;

        $where = array();
        if(!empty($directory)){
            $where["directory"] = $directory;
        }
        if($is_me){
            $loginUser = getLoginUser();
            $where["uid"] = $loginUser["uid"];
        }
        if($category == 1){
            $where["category"] = 0;
        }else if($category == 2){
            $where["category"] = 1;
        }
        $imgList = $this->ImgLibModel->getList($where, $page, $pagesize);
        $count = $this->ImgLibModel->getListCnt($where);

        $url = 'index.php?c=imgLib&m=ajax_img_list&id='.$id.'&directory='.$directory."&is_me=".$is_me."&category=".$category;
        $multipage = ajax_multipage($count, $page, $pagesize, $url, "data_img_list", "page_img_list");

        $html = "";
        foreach($imgList as $img){
            if($img["category"] == 1){
                $cls = "img_d_1";$w = "100px";$h = "100px";
            }else{
                $cls = "img_d_2";$w = "160px";$h = "90px";
            }
            $html.= "<div class=\"img_d\">";
            $html.= "   <a href=\"javascript:void(0)\" onclick=\"check_img('".$id."','".$img["path"]."')\" class=\"".$cls."\"><img src=\"".IMG_URL.$img["path"]."\" width=\"".$w."\" height=\"".$h."\"></a>";
            $html.= "</div>";
        }
        $html.= "<div class=\"clearfix\"></div>";

        $multipage.= "<div class=\"clearfix\"></div>";

        std_out(0,'',array("data_list"=>$html,'multipage'=>$multipage));
    }
}